package com.shop.shoppingmall.service.impl;

import com.shop.shoppingmall.dao.ShShipAddressDao;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShShipAddress;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShShipAddressService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 收货地址(ShShipAddress)表服务实现类
 *
 * @author makejava
 * @since 2020-09-09 11:34:54
 */
@Service("shShipAddressService")
@Transactional
public class ShShipAddressServiceImpl implements ShShipAddressService {
    @Resource
    private ShShipAddressDao shShipAddressDao;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ShShipAddress queryById(String id) {
        return this.shShipAddressDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ShShipAddress> queryAllByLimit(int offset, int limit) {
        return this.shShipAddressDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shShipAddress 实例对象
     * @return 实例对象
     */
    @Override
    public ShShipAddress insert(ShShipAddress shShipAddress) {
        this.shShipAddressDao.insert(shShipAddress);
        return shShipAddress;
    }

    /**
     * 修改数据
     *
     * @param shShipAddress 实例对象
     * @return 实例对象
     */
    @Override
    public ShShipAddress update(ShShipAddress shShipAddress) {
        this.shShipAddressDao.update(shShipAddress);
        return this.queryById(shShipAddress.getId());
    }

    /**
     * 添加地址
     * @param shShipAddress
     * @return
     */
    @Override
    public Result addShopAddress(ShShipAddress shShipAddress, ShUser shUser) {
        //自动生成id
        String id = UUIDGenerator.generate();
        shShipAddress.setId(id);
        shShipAddress.setUserId(shUser.getId());
        if(shShipAddress.getStatus().equals("0")){
            shShipAddressDao.insert(shShipAddress);
            return Result.ok("添加成功！");
        }else{
            ShShipAddress shipAddress = shShipAddressDao.selectAddressId(shUser.getId());
            if(shipAddress != null){
                shipAddress.setStatus("0");
                shShipAddressDao.update(shipAddress);
                shShipAddressDao.insert(shShipAddress);
                return Result.ok("添加成功!");
            }else{
                shShipAddressDao.insert(shShipAddress);
                return Result.ok("添加成功！");
            }
        }
    }

    /**
     * 修改收货地址
     * @param shShipAddress
     * @return
     */
    @Override
    public Result updateShopAddress(ShShipAddress shShipAddress,String userId) {
        if(shShipAddress.getStatus().equals("0")){
            shShipAddressDao.update(shShipAddress);
            return Result.ok("修改成功！");
        }else {
            ShShipAddress shipAddress = shShipAddressDao.selectAddressId(userId);
            if(shipAddress != null){
                shipAddress.setStatus("0");
                shShipAddressDao.update(shipAddress);
                shShipAddressDao.update(shShipAddress);
                return Result.ok("修改成功！");
            }else{
                shShipAddressDao.update(shShipAddress);
                return Result.ok("修改成功！");
            }
        }
    }

    /**
     * 查询该用户所有的收货地址
     * @param request
     * @return
     */
    @Override
    public Result selectShopAddress(HttpServletRequest request) {
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String userId = shUser.getId();
        List<ShShipAddress> shShipAddresses = shShipAddressDao.selectShipAddress(userId);
        if(shShipAddresses.size() == 0){
            return Result.error("您还没有添加地址！");
        }
        return Result.ok(shShipAddresses);
    }


    @Override
    public Result deleteShopAddress(String addId) {
       shShipAddressDao.deleteById(addId);
        return Result.ok("删除成功");
    }

    @Override
    public Result selectByAddress(String addId) {
        ShShipAddress shShipAddress = shShipAddressDao.queryById(addId);
        JSONObject jsons = JSONObject.fromObject(shShipAddress);
        return Result.ok(jsons);
    }

    /**
     * 查询该用户的默认地址
     * @param request
     * @return
     */
    @Override
    public Result selectAddressDeFault(HttpServletRequest request) {
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        String userId = shUser.getId();
        ShShipAddress shipAddress = shShipAddressDao.selectAddressDeFault(userId);
        if(shipAddress == null){
            return Result.error("暂时还没有默认地址信息，请设置！");
        }
        return Result.ok(shipAddress);
    }

}